﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.UI.Views;
using StockFinder.Model;
using StockFinder.Services.Engines.Yahoo;
using StockFinder.Services.Engines;
using System.Threading;
using StockFinder.DataAccess;
using StockFinder.DTO;

namespace StockFinder.UI.Controllers
{
    public class UrlsListViewController
    {
        private readonly IUrlsListView _View;
        private readonly ISymbolIndexUrlExtractor _SymbolIndexUrlExtractor;
        private readonly IndexUrlDataAccess _IndexUrlDataAccess;

        public UrlsListViewController(IUrlsListView view)
        {
            _View = view;

            _SymbolIndexUrlExtractor = new YahooSymbolIndexUrlExtractEngine();
            _IndexUrlDataAccess = new IndexUrlDataAccess();

            _View.Start += new EventHandler(OnViewStart);
            _View.InsertUrls += new EventHandler(OnViewInsertUrls);
            _View.LoadFromDatabase += new EventHandler(OnViewLoadFromDatabase);
            _SymbolIndexUrlExtractor.LogUpdate += new Action<string>(_SymbolIndexUrlExtractor_LogUpdate);
        }

        void OnViewLoadFromDatabase(object sender, EventArgs e)
        {
            //load the indexurls from database
            List<IndexUrl> indexUrls = _IndexUrlDataAccess.GetAllIndexUrls();

            //convert to DTOs
            List<IndexUrlDTO> indexUrlDTOs = indexUrls.Select(indexUrl => new IndexUrlDTO()
            {
                IndexUrlId = indexUrl.IndexUrlId, Title = indexUrl.Title, Url = indexUrl.Url
            }).ToList();

            //set view
            _View.SetIndexUrlsList(indexUrlDTOs);
        }

        void OnViewInsertUrls(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// Occurs when the symbol index url extractor has an update
        /// </summary>
        /// <param name="obj"></param>
        void _SymbolIndexUrlExtractor_LogUpdate(string logMessage)
        {
            _View.AppendLog(logMessage);
        }

        /// <summary>
        /// View requested to start getting Urls from service
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnViewStart(object sender, EventArgs e)
        {
            //invoke this in separate thread to stop UI from hanging
            Thread thread = new Thread(new ThreadStart(delegate
            {
                List<IndexUrl> urls = _SymbolIndexUrlExtractor.GetUrls();

                //convert to DTOs
                List<IndexUrlDTO> indexUrlDTOs = urls.Select(indexUrl => new IndexUrlDTO()
                {
                    IndexUrlId = indexUrl.IndexUrlId,
                    Title = indexUrl.Title,
                    Url = indexUrl.Url
                }).ToList();

                //set view
                _View.SetIndexUrlsList(indexUrlDTOs);
            }));

            thread.Name = "YahooSymbolIndexUrlExtractEngineThread";
            thread.Start();            
        }
    }
}
